package com.jscloud.bigdata.flink.demo1;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.FlatMapOperator;
import org.apache.flink.api.java.operators.MapOperator;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

public class WordCountBatchJava {
        public static void main(String[] args) throws Exception {
                //使用批量处理，程序的入口是叫做 ExecutionEnvironment
                ExecutionEnvironment executionEnvironment = ExecutionEnvironment.getExecutionEnvironment();
                //executionEnvironment.setParallelism(4);  //执行环境级别设置
                DataSource<String> fileSource = executionEnvironment.readTextFile("D:\\JSProjects\\jsCloud-bigdata-app\\datas\\hello.txt");

                FlatMapOperator<String, String> words = fileSource.flatMap(new FlatMapFunction<String, String>() {
                        @Override
                        public void flatMap(String s, Collector<String> collector) throws Exception {
                                String[] eachWords = s.split(" ");
                                for (String eachWord : eachWords) {
                                        collector.collect(eachWord);
                                }

                        }
                });

                MapOperator<String, Tuple2<String, Integer>> wordAndOne = words.map(new MapFunction<String, Tuple2<String, Integer>>() {
                        @Override
                        public Tuple2<String, Integer> map(String s) throws Exception {
                                Tuple2<String, Integer> tuple2 = new Tuple2<>(s, 1);
                                tuple2.setFields(s, 1);
                                return tuple2;
                        }
                }); //.setParallelism(6) 算子级别并行度设置

                AggregateOperator<Tuple2<String, Integer>> result = wordAndOne.groupBy(0).sum(1);

                result.print();

                //executionEnvironment.execute(); 加上执行语句  会报错
                //Exception in thread "main" java.lang.RuntimeException: No new data sinks have been defined since the last execution. The last execution refers to the latest call to 'execute()', 'count()', 'collect()', or 'print()'.
                //	at org.apache.flink.api.java.ExecutionEnvironment.createProgramPlan(ExecutionEnvironment.java:1165)
                //	at org.apache.flink.api.java.ExecutionEnvironment.createProgramPlan(ExecutionEnvironment.java:1145)
                //	at org.apache.flink.api.java.ExecutionEnvironment.executeAsync(ExecutionEnvironment.java:1041)
                //	at org.apache.flink.api.java.ExecutionEnvironment.execute(ExecutionEnvironment.java:958)
                //	at org.apache.flink.api.java.ExecutionEnvironment.execute(ExecutionEnvironment.java:942)
                //	at com.jscloud.bigdata.flink.demo1.WordCountBatchJava.main(WordCountBatchJava.java:44)


        }
}
